package com.xdd.dao.config.db.lucifer;

import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.xdd.dao.config.db.AbstractDbConfig;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.PlatformTransactionManager;

import java.sql.SQLException;

@Configuration
public class LuciferDbConfig extends AbstractDbConfig
{
	protected final String beanPrefix = "lucifer";

	@Autowired
	private LuciferResourceConfig luciferResourceConfig;

	@Bean(name = beanPrefix + "StatFilter")
	protected StatFilter statFilter()
	{
		return super.statFilter(luciferResourceConfig);
	}

	@Bean(name = beanPrefix + "Slf4jFilter")
	protected Slf4jLogFilter Slf4jFilter()
	{
		return super.Slf4jFilter(luciferResourceConfig);

	}

	/**
	 * 多数据源时请设置spring.datasource.initialize=false或者添加一个@Primary注解。
	 * Spring Boot的auto-configuration特性(DataSourceInitializer)允许应用启动时执行一些SQL脚本。该特性在执行之前需要获取数据源，而存在多个数据源时代码中并没有指定获取哪一个，所以应用无法启动。
	 * 这应该是Spring Boot的一个BUG，BUG代码如下：
	 * <pre class="code">
	 * 	&#064;PostConstruct
	 *	public void init() {
	 *		if (!this.properties.isInitialize()) {
	 *			logger.debug("Initialization disabled (not running DDL scripts)");
	 *			return;
	 *        }
	 *		if (this.applicationContext.getBeanNamesForType(DataSource.class, false, false).length > 0) {
	 *			this.dataSource = this.applicationContext.getBean(DataSource.class);
	 *        }
	 *		if (this.dataSource == null) {
	 *			logger.debug("No DataSource found so not initializing");
	 *			return;
	 *        }
	 *		runSchemaScripts();
	 *    }
	 *	</pre>
	 */
	@Bean(name = beanPrefix + "DataSource")
	@Primary   //TODO  为什么必须加这个注解
	protected DruidDataSource dataSource(
			@Qualifier(beanPrefix + "StatFilter") StatFilter statFilter,
			@Qualifier(beanPrefix + "Slf4jFilter") Slf4jLogFilter Slf4jFilter) throws SQLException
	{
		return super.dataSource(luciferResourceConfig, statFilter, Slf4jFilter);
	}

	@Override
	@Bean(name = beanPrefix + "SqlSessionFactory")
	protected SqlSessionFactory sqlSessionFactory(@Qualifier(beanPrefix + "DataSource") DruidDataSource dataSource)
			throws Exception
	{
		logger.info("数据源启动完成:" + dataSource.getName());
		return super.sqlSessionFactory(dataSource);
	}

	@Override
	@Bean(name = beanPrefix + "SqlSessionTemplate")
	protected SqlSessionTemplate sqlSessionTemplate(
			@Qualifier(beanPrefix + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory)
	{
		return super.sqlSessionTemplate(sqlSessionFactory);

	}

	@Override
	@Bean(name = beanPrefix + "TransactionManager")
	protected PlatformTransactionManager transactionManager(
			@Qualifier(beanPrefix + "DataSource") DruidDataSource dataSource) throws SQLException
	{
		return super.transactionManager(dataSource);
	}
}